package com.qianchang.microfinance.utils;

import android.Manifest;
import android.content.Context;
import android.content.SharedPreferences;
import android.content.pm.PackageManager;
import android.net.wifi.WifiInfo;
import android.net.wifi.WifiManager;
import android.os.Build;
import android.support.v4.app.ActivityCompat;
import android.telephony.TelephonyManager;
import android.text.TextUtils;

import java.util.UUID;

import static android.content.Context.MODE_PRIVATE;

/**
 * Created by SSC on 2017/12/26.
 */

public class DeviceIdUtils {


    public String getMacAddress(Context context) {
        String macAddress = null;
        WifiManager wifiManager = (WifiManager) context.getSystemService(Context.WIFI_SERVICE);
        WifiInfo info = (null == wifiManager ? null : wifiManager.getConnectionInfo());
        if (!wifiManager.isWifiEnabled()) {
            //必须先打开，才能获取到MAC地址
            wifiManager.setWifiEnabled(true);
            wifiManager.setWifiEnabled(false);
        }
        if (null != info) {
            macAddress = info.getMacAddress();
        }
        return macAddress;
    }

    //获取手机的唯一标识
    public String getPhoneSign(Context context) {
        StringBuilder deviceId = new StringBuilder();
        // 渠道标志
        deviceId.append("a");
        try {
            //IMEI（imei）
            TelephonyManager tm = (TelephonyManager) context.getSystemService(Context.TELEPHONY_SERVICE);
            if (ActivityCompat.checkSelfPermission(context, Manifest.permission.READ_PHONE_STATE) != PackageManager.PERMISSION_GRANTED) {
                return deviceId.toString();
            }
            String imei = tm.getDeviceId();
            if (!TextUtils.isEmpty(imei)) {
                deviceId.append("imei");
                deviceId.append(imei);
                return deviceId.toString();
            }
            //序列号（sn）
            String sn = tm.getSimSerialNumber();
            if (!TextUtils.isEmpty(sn)) {
                deviceId.append("sn");
                deviceId.append(sn);
                return deviceId.toString();
            }
            //如果上面都没有， 则生成一个id：随机码
            String uuid = getUUID(context);
            if (!TextUtils.isEmpty(uuid)) {
                deviceId.append("id");
                deviceId.append(uuid);
                return deviceId.toString();
            }
        } catch (Exception e) {
            e.printStackTrace();
            deviceId.append("id").append(getUUID(context));
        }
        return deviceId.toString();
    }

    /**
     * 36   * 得到全局唯一UUID
     * 37
     */
    private String uuid;
    public String getUUID(Context context) {
        SharedPreferences mShare = context.getSharedPreferences("uuid", MODE_PRIVATE);
        if (mShare != null) {
            uuid = mShare.getString("uuid", "");
        }
        if (TextUtils.isEmpty(uuid)) {
            uuid = UUID.randomUUID().toString();
            mShare.edit().putString("uuid", uuid).commit();
        }
        return uuid;
    }


    /***
     * 通过取出ROM版本、制造商、CPU型号、以及其他硬件信息来实现 设备唯一值
     */

    public static String getDiviceStr() {
        String m_szDevIDShort = "35" +                          //we make this look like a valid IMEI
                Build.BOARD.length() % 10 +                     //主板
                Build.BRAND.length() % 10 +                     //android系统定制商
                Build.CPU_ABI.length() % 10 +                   // cpu指令集
                Build.DEVICE.length() % 10 +                    // 设备参数
                Build.DISPLAY.length() % 10 +                   // 显示屏参数
                Build.HOST.length() % 10 +                      // 硬件名称
                Build.ID.length() % 10 +                        // 修订版本列表
                Build.MANUFACTURER.length() % 10 +              // 硬件制造商
                Build.MODEL.length() % 10 +                     // 版本
                Build.PRODUCT.length() % 10 +                   // 手机制造商
                Build.TAGS.length() % 10 +                      // 描述build的标签
                Build.TYPE.length() % 10 +                      // builder类型
                Build.USER.length() % 10;                       //13 digit
        return m_szDevIDShort;
    }





}
